<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:fn="http://java.sun.com/jsp/jstl/functions"
	xmlns:p="http://primefaces.org/ui"
	xmlns:cc="http://java.sun.com/jsf/composite"
	xmlns:c="http://java.sun.com/jsp/jstl/core"
	xmlns:hmrList="http://java.sun.com/jsf/composite/list"	
	xmlns:hmrDetail="http://java.sun.com/jsf/composite/detail"		>

<cc:interface>
	<cc:attribute name="handler"			type="com.hemerasolutions.v2trainer.component.interfaces.table.ExerciseTableCompInterface" required="true"/>
	<cc:attribute name="editable"			type="java.lang.Boolean"	default="false"	 	required="true"/>	
	<cc:attribute name="updateOnSelect"		type="java.lang.String" 	default=""			required="false"/>	
	<cc:attribute name="mode"				type="java.lang.String" 	default="manage"	required="false"/>
</cc:interface>

<cc:implementation>
	<span id="#{cc.clientId}" >
		<p:panel id="exercisesPanel" header="#{msgs.Exercises} #{msgs.of} #{msgs.Modality} #{cc.attrs.handler.modality.name}" style="width:100%;" >
			<h:form id="modalityExercisesForm" prependId="false">
				<p:dataTable id="exercisesTable" value="#{cc.attrs.handler.filteredExercises}" var="versionableExercise" paginator="true"  
					paginatorAlwaysVisible="false"  paginatorPosition="bottom" rows="8" emptyMessage="#{msgs.NoResults}" rowStyleClass="#{versionableExercise == cc.attrs.handler.versionableExercise ? 'ui-state-highlight' : null}">   
					<p:columnGroup type="header" >  
						<p:row>
							<p:column colspan="#{cc.attrs.mode == 'manage'?9:8}" style="text-align:center;">
								<f:facet name="header">
									<h:outputText value="#{msgs.ModalityExercises}" />
								</f:facet>
							</p:column>
							<p:column colspan="1"  styleClass="ui-table-button-row">
								<f:facet name="header">
									<p:commandButton actionListener="#{cc.attrs.handler.toFilterExercises}" update=":#{cc.clientId}:filterExerciseDialog"   oncomplete="wfilterExerciseDialog_#{cc.id}.show();" title="#{msgs.Filter}" icon="ui-icon-search"  styleClass="ui-table-button"/>
								</f:facet>
							</p:column>
						</p:row>
						<p:row>
							<p:column colspan="1"  styleClass="ui-table-button-row" rendered="#{cc.attrs.mode == 'manage'}" >
								<f:facet name="header">
									<p:commandButton action="#{cc.attrs.handler.toAddExercise}" process="@this" disabled="#{!cc.attrs.editable}"
										update=":#{cc.clientId}:exerciseDialog" oncomplete="wexerciseDialog_#{cc.id}.show();"  title="#{msgs.Add}" icon="ui-icon-plus" styleClass="ui-table-button"/>
								</f:facet>
							</p:column>		
							<p:column	colspan="3" >
								<f:facet name="header">
									<h:outputText value="#{msgs.Name}"/>
									
								</f:facet>
							</p:column>
							<p:column	colspan="1" style="padding:0px;margin:0px;">
								<f:facet name="header">
									<h:outputText value="#{msgs.Description}"/>
								</f:facet>
							</p:column>
							<p:column	colspan="1" style="padding:0px;margin:0px;min-width:12px;max-width:12px">
								<f:facet name="header">
									<h:outputText value="#{msgs.Version}"/>
								</f:facet>
							</p:column>

							<p:column	colspan="1" style="padding:0px;margin:0px;">
								<f:facet name="header">
									<h:outputText value="#{msgs.BodyRegion}"/> 
								</f:facet>
							</p:column>
							<p:column	colspan="1" style="padding:0px;margin:0px;">
								<f:facet name="header">
									<h:outputText value="#{msgs.MuscleGroup}"/>
								</f:facet>
							</p:column>
							<p:column	colspan="2" style="padding:0px;margin:0px;">
								<f:facet name="header">
									<h:outputText value="#{msgs.CreatedAt}"/>
								</f:facet>
							</p:column>		
						</p:row>	 
					</p:columnGroup>


					<p:column colspan="1" styleClass="ui-table-button-row" rendered="#{cc.attrs.mode == 'manage'}"  >
						<p:commandButton  title="#{msgs.View}" icon="ui-icon-search"  action="#{cc.attrs.handler.toViewExercise}"
							update=":#{cc.clientId}:exerciseDialog"  oncomplete="PF('wexerciseDialog_#{cc.id}').show();" styleClass="ui-table-button">
							<f:setPropertyActionListener target="#{cc.attrs.handler.versionableExercise}" 				value="#{versionableExercise}"/>
						</p:commandButton>
					</p:column>


					<p:column colspan="#{cc.attrs.mode == 'manage'?1:2}" headerText="#{msgs.Name}" >
						<h:outputText value="#{versionableExercise.currentVersion.name}" />
					</p:column>
		
		
					<p:column colspan="1" styleClass="ui-table-button-row" rendered="#{cc.attrs.mode == 'manage'}"  >
						<p:commandButton title="#{msgs.IntensityZones}" action="#{cc.attrs.handler.toAdjustIntensitys}" update="exercisesTable, :#{cc.clientId}:panelIntensitys, :growl,"   icon="ui-icon-bookmark" styleClass="ui-table-button" >
							<f:setPropertyActionListener target="#{cc.attrs.handler.versionableExercise}" value="#{versionableExercise}"/>
						</p:commandButton>
					</p:column>		
		
					<p:column colspan="1" styleClass="ui-table-button-row"  rendered="#{cc.attrs.mode == 'manage'}" >
						<p:commandButton title="#{msgs.Delete}" process="@this"  oncomplete="wconfExcExercise_#{cc.id}.show();" 
							disabled="#{!cc.attrs.editable}" icon="ui-icon-trash" styleClass="ui-table-button" >
							<f:setPropertyActionListener target="#{cc.attrs.handler.versionableExercise}" value="#{versionableExercise}"/>
						</p:commandButton>
					</p:column>		
					
					<p:column colspan="1" styleClass="ui-table-button-row"  rendered="#{cc.attrs.mode == 'select'}" >
						<p:commandButton title="#{msgs.Add}"  action="#{cc.attrs.handler.selectVersionableExercise}" icon="ui-icon-plus" styleClass="ui-table-button"
							update="#{cc.attrs.updateOnSelect}" >
							<f:setPropertyActionListener target="#{cc.attrs.handler.versionableExercise}" value="#{versionableExercise}"/>
						</p:commandButton>
					</p:column>							
					

					<p:column headerText="#{msgs.Description}" >
						<h:outputText value="#{versionableExercise.exercise.description}" />
					</p:column>		
		
					<p:column headerText="#{msgs.Version}" style="padding:1px;width:15px;">
						<p:selectOneMenu value="#{versionableExercise.exercise}" converter="#{genericConverter}"  style="white-space: nowrap;min-width:60px;max-width:60px;"  
							styleClass="ui-selector-on-table"  >
							<f:selectItems value="#{versionableExercise.exerciseVersions}" var="exerciseVersion"  
								itemLabel="#{exerciseVersion.version}" itemValue="#{exerciseVersion}"/>
							<p:ajax  update="exercisesTable" />
						</p:selectOneMenu>	
					</p:column>
					
					<p:column headerText="#{msgs.BodyRegion}" >
						<h:outputText value="#{versionableExercise.exercise.bodyRegionsText}" />
					</p:column>		
		
					<p:column headerText="#{msgs.MuscleGroup}" >
						<h:outputText value="#{versionableExercise.exercise.muscleGroupsText}" />
					</p:column>		
		
					<p:column colspan="2" headerText="#{msgs.CreatedAt}">
						<h:outputText value="#{versionableExercise.exercise.createdAt}">
							<f:convertDateTime locale="pt" dateStyle="long" pattern="dd/MM/yyyy HH:mm" />
						</h:outputText>
					</p:column>		
				</p:dataTable>	
			</h:form>
		</p:panel>
		
		<p:outputPanel id="panelIntensitys" layout="block"  style="width:100%;padding-bottom:20px">
			<p:panel  header="#{ msgs.Intensitys} - #{cc.attrs.handler.exercise.name} - #{msgs.Version}: #{cc.attrs.handler.exercise.version} " rendered="#{cc.attrs.handler.exercise != null}"   style="margin-top:10px"  >
				<hmrList:intensityZoneList handler="#{cc.attrs.handler.izList}" editable="#{cc.attrs.editable}"/>	
			</p:panel>
		</p:outputPanel>
	</span>
	
	<p:dialog id="filterExerciseDialog" header="#{msgs.FindExercises}" modal="true" appendTo="@(body)"  widgetVar="wfilterExerciseDialog_#{cc.id}">
		<h:form id="findExerciseForm" prependId="false">
			<h:panelGrid columns="4" cellpadding="2">
				<h:outputLabel 	value="#{msgs.Name}: " 	 />
				<p:inputText	value="#{ cc.attrs.handler.filterExerciseByName}" 			maxlength="50" 	size="20" />
				<p:spacer/>
				<p:spacer/>

				<h:outputLabel 	value="#{msgs.Description}: " />
				<p:inputText	value="#{ cc.attrs.handler.filterExerciseByDescription}"		maxlength="100" 	size="45" />
				<p:spacer/>
				<p:spacer/>
 
	            <h:outputLabel value="#{msgs.Technical}: " />
				<p:selectBooleanCheckbox  value="#{cc.attrs.handler.filterExerciseByTechnical}"  />   
				<p:spacer/>
				<p:spacer/>
 
	            <h:outputLabel value="#{msgs.ModalityDefault}: " />
				<p:selectBooleanCheckbox  value="#{cc.attrs.handler.filterExerciseByDefault}"  />   
				<p:spacer/>
				<p:spacer/>

				<h:outputText value="#{msgs.BodyRegions}: "  />
	 			<p:selectCheckboxMenu id="bodyRegionSelect" value="#{cc.attrs.handler.filterExerciseByBodyRegions}" converter="#{genericConverter}" 
					filter="true" panelStyle="width:220px" label="#{msgs.BodyRegions}" > 
		          	<f:selectItems value="#{cc.attrs.handler.fullBodyRegions}" var="bodyRegion" itemLabel="#{msgs[bodyRegion.name]}" itemValue="#{bodyRegion}" />
					<p:ajax  listener="#{cc.attrs.handler.changeFullBodyRegions}" update="muscleGroupSelect"/>
		        </p:selectCheckboxMenu>

				<h:outputText value="#{msgs.MuscleGroups}: "  />
	 			<p:selectCheckboxMenu id="muscleGroupSelect" value="#{cc.attrs.handler.filterExerciseByMuscleGroups}" converter="#{genericConverter}" 
					filter="true" panelStyle="width:220px" label="#{msgs.MuscleGroups}" disabled="#{empty cc.attrs.handler.fullMuscleGroups}" >
					<f:selectItems value="#{cc.attrs.handler.fullMuscleGroups}" var="muscleGroup" itemLabel="#{msgs[muscleGroup.name]}" itemValue="#{muscleGroup}" />
					<p:ajax listener="#{cc.attrs.handler.changeFullMuscleGroups}" update="muscleSelect"/>
		        </p:selectCheckboxMenu>
		        
				<h:outputText  value="#{msgs.Executions}" />
				<p:selectCheckboxMenu id="executionTypeSelect" value="#{cc.attrs.handler.filterExerciseByExecutions}" converter="#{genericConverter}" 
					filter="true" panelStyle="width:220px" label="#{msgs.Executions}" disabled="#{empty cc.attrs.handler.fullExecutions}" >
	            	<f:selectItems value="#{cc.attrs.handler.fullExecutions}"  var="execution" itemLabel="#{msgs[execution.name]}" itemValue="#{execution}" />
				</p:selectCheckboxMenu>

				<h:outputText value="#{msgs.Muscles}: " />
	 			<p:selectCheckboxMenu id="muscleSelect" value="#{cc.attrs.handler.filterExerciseByMuscles}" converter="#{genericConverter}" 
					filter="true" panelStyle="width:220px" label="#{msgs.Muscles}" disabled="#{empty cc.attrs.handler.fullMuscles}" > 
		          	<f:selectItems value="#{cc.attrs.handler.fullMuscles}" var="muscle" itemLabel="#{msgs[muscle.name]}" itemValue="#{muscle}" />
		        </p:selectCheckboxMenu>

			</h:panelGrid>		
 
			<p:commandButton value="#{ msgs.Search}"	actionListener="#{cc.attrs.handler.filterExercises}" oncomplete="wfilterExerciseDialog_#{cc.id}.hide();"
				update=":growl, :#{cc.clientId}:modalityExercisesForm " 	icon=" ui-icon-search"  process="@form" />
			<p:commandButton value="#{ msgs.Clear}"		actionListener="#{cc.attrs.handler.reloadExercises}"	icon="ui-icon-clean" update=":growl">
				 <p:ajax update="findExerciseForm" resetValues="true" />  	
			</p:commandButton>

		</h:form>	
	</p:dialog>

	<p:dialog id="exerciseDialog" header="#{msgs.Exercise}" modal="true" appendTo="@(body)" widgetVar="wexerciseDialog_#{cc.id}" style="width:90%"> 

		<script type="text/javascript">
		    function handleExerciseDialogSubmit(xhr, status, args) {
		        if (args.validationFailed) {
		        	PF('wexerciseDialog_#{cc.id}').show();
		        } else {
		        	PF('wexerciseDialog_#{cc.id}').hide();
		        }
		    }
		 </script>  

		<p:outputPanel layout="block">
		
			<hmrDetail:exerciseDetails handler="#{cc.attrs.handler.exerciseDetailsComp}"
				editable="#{cc.attrs.editable}"
				oncomplete="handleExerciseDialogSubmit(xhr, status, args);"
				oncancelclick="wexerciseDialog_#{cc.id}.hide()" 
				saveExerciseListener="#{cc.attrs.handler.saveExercise}"
				update=":growl, :#{cc.clientId}"    />
		</p:outputPanel>
	</p:dialog>	

	<p:confirmDialog  message="#{msgs.DoYouReallyWantToDeleteThisExerciseAndIntesitys}" appendTo="@(body)" 	header="#{msgs.Warning}" severity="alert" widgetVar="wconfExcExercise_#{cc.id}">
		<h:form prependId="false">
			<p:commandButton value="#{msgs.Yes}" oncomplete="wconfExcExercise_#{cc.id}.hide();"	process="@form" actionListener="#{cc.attrs.handler.deleteExercise}"
				update=" :growl, :#{cc.clientId}:modalityExercisesForm, :#{cc.clientId}:panelIntensitys"  /> 
			<p:commandButton value="#{msgs.No}" onclick="wconfExcExercise_#{cc.id}.hide();"	type="button" />
		</h:form>
	</p:confirmDialog>


</cc:implementation>


</html>